<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.


function admin_login(){
    if (!isset($_SERVER['PHP_AUTH_USER'])) {
        header('WWW-Authenticate: Basic realm="'._("Please login with your username and password").'"');
        header('HTTP/1.0 401 Unauthorized');
        die();
    } else {
        global $sql_hostname, $sql_database, $admin, $sql_username, $sql_password;
        try {
            $db = new PDO( "mysql:host=$sql_hostname;dbname=$sql_database", $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] );
            $sql_username = $_SERVER['PHP_AUTH_USER'];
            $sql_password = $_SERVER['PHP_AUTH_PW'];
        }
        catch(PDOException $e) {
            header('WWW-Authenticate: Basic realm="'._("Please login with your username and password").'"');
            header('HTTP/1.0 401 Unauthorized');
            die("Please provide username and password! ${e}");
        }
    }
}

function get_template_admin(){
    global $admin ,$edit, $translate, $lang, $site_head_extra, $main_site_title, $main_site_image, $main_site_gtrans, $main_site_mail, $template, $site_dir, $site;
    if ($admin){
        global $template_admin, $template_top;
        $link = urlencode(construct_link(array("page","action")));
        $edit_link = "admin/add-page.php?url=$link&site=$site";
        $template_admin = "<div class=\"admin\">
            <a href=\"$edit_link\">"._("Add page")."</a> |
            <a href=\"#\" onclick=\"show_configuration()\">"._("Configure site")."</a> |
            "._("Logged in as {$_SERVER['PHP_AUTH_USER']}")."</div><div id=\"admin-blanket\" style=\"display: none;\"></div><div id=\"admin-configure\" style=\"display: none;\"></div>";

        $save_conf_link = construct_link(array("action"))."action=save_conf";

        // in this include we define extensive html like configure_html and imagebrowser_html
        include("admin/admin-extra.php");

        $template_top .= $template_admin;

        if (!$edit or !$translate){
            global $template_title;
            $edit_link = construct_link(array("action"))."action=edit";
            $tr_link = construct_link(array("action"))."action=translate";
            $remove_link = construct_link(array("action"))."action=remove";
            $script = "<script type=\"text/javascript\" language=\"Javascript\">
      function admin_hide(id) {
          document.getElementById(id).style.display = 'none';
      }
      function admin_show(id) {
          document.getElementById(id).style.display = 'block';
      }
      function confirm_remove(){
        if(confirm('"._("Are you sure you want to remove this page")."'))
            location.href='$remove_link';
      }

    </script> ";
            $template_title = "$script
            <div class=\"admin-title\" onmouseover=\"admin_show('admin-menu');\" onmouseout=\"admin_hide('admin-menu');\">
                $template_title
                <div id=\"admin-menu\" class=\"admin-title-menu\">
                    <a href=\"$edit_link\">"._("Edit")."</a> ";

            if ( $lang != "en" ){
                $template_title .= "| <a href=\"$tr_link\">"._("Translate")."</a>";
            }
            $template_title .= "
            | <a href=\"javascript:confirm_remove()\">"._("Remove")."</a>
                </div>
            </div>
            <script type=\"text/javascript\" language=\"Javascript\">hide('admin-menu');</script>
            ";
        }
    }
}
?>
